package cn.aimee.permission.util.permission;

import android.Manifest;
import android.content.Context;

import cn.aimee.permission.R;
import cn.aimee.permission.util.string.StringUtil;


public class PermissionDescriptionUtil {

    /**app需要的权限*/
    public static final String[] appNeedPermissions = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    /**调用摄像头需要的权限*/
    public static final String[] cameraPermissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE};

    /**使用SD卡需要的权限*/
    public static final String[] storagePermissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};

    /**
     * 获取相关权限的描述，用于给用户提示，以便引导用户开启相关权限
     * @param context
     * @param permission
     * @return
     */
    public static String get(Context context, String permission) {
        String description = context.getResources().getString(R.string.str_default_permission_description);
        if (Manifest.permission.READ_PHONE_STATE.equals(permission)) {
            description = context.getResources().getString(R.string.str_read_phone_state);
        } else if (Manifest.permission.READ_EXTERNAL_STORAGE.equals(permission) ||
                Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permission)) {
            description = getNormalDes(context, R.string.str_permissions_storage);
        }
        return description;
    }

    /**
     * 获取多个权限的描述，用于给用户提示，以便引导用户开启相关权限
     * @param context
     * @param permissions
     * @return
     */
    public static String gets(Context context, String[] permissions) {
        String defDescription = context.getResources().getString(R.string.str_default_permission_description);
        if (null == permissions || permissions.length == 0) {
            return defDescription;
        }
        String description = "";
        for (int i=0; i<permissions.length; i++) {
            if (Manifest.permission.READ_PHONE_STATE.equals(permissions[i])) {
                description = context.getResources().getString(R.string.str_read_phone_state);
            } else if (Manifest.permission.READ_EXTERNAL_STORAGE.equals(permissions[i]) ||
                    Manifest.permission.WRITE_EXTERNAL_STORAGE.equals(permissions[i])) {
                description = getPermissionsDes(context, description, R.string.str_permissions_storage);
            }
        }
        if (StringUtil.isNotEmpty(description)) {
            description = getNormalDes(context, description);
        } else {
            description = defDescription;
        }
        return description;
    }

    /**
     * 获取连接后的权限描述，主要是在多个权限描述中添加“、”
     * @param context
     * @param prePermissionDes 前面的权限描述
     * @param curPermissionDes 当前的权限描述
     * @return
     */
    private static String getPermissionsDes(Context context, String prePermissionDes, int curPermissionDes) {
        if (StringUtil.isNotEmpty(prePermissionDes)) {
            prePermissionDes = prePermissionDes + "、";
        }
        return prePermissionDes + context.getResources().getString(curPermissionDes);
    }

    private static String getNormalDes(Context context, int desId) {
        return getNormalDes(context, context.getResources().getString(desId));
    }

    private static String getNormalDes(Context context, String des) {
        String description = String.format(context.getResources().getString(R.string.str_no_permissions), des);
        return description;
    }

}